package com.company.test30;


import javax.swing.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by v on 16-12-1.
 */
public class TicTacToeServer implements TicTacToeConstants{

    public TicTacToeServer(){
        System.out.println("structure……");
        try {
            ServerSocket serverSocket=new ServerSocket(8000);
            while (true){
                System.out.println("waiting player 1...");
                Socket socket1 = serverSocket.accept();
                System.out.println("waiting player 2...");
                Socket socket2 = serverSocket.accept();

                HandleASession handleASession = new
                        HandleASession(socket1, socket2);

                Thread thread = new Thread(handleASession);
                thread.start();
            }
        }catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    class HandleASession implements Runnable,TicTacToeConstants{
        private int[][] map=new int[3][3];

        private Socket socket1;
        private Socket socket2;

        DataInputStream inputPlayer1;
        DataInputStream inputPlayer2;
        DataOutputStream outputPlayer1;
        DataOutputStream outputPlayer2;

        //轮到谁
        private int turn;
        //下什么棋
        private int target=O;
        //棋盘状态
        private int state=CONTINUE;

        public HandleASession(Socket socket1,Socket socket2){
            this.socket1=socket1;
            this.socket2=socket2;
        }

        @Override
        public void run() {
            try{
                inputPlayer1=new DataInputStream(socket1.getInputStream());
                inputPlayer2=new DataInputStream(socket2.getInputStream());
                outputPlayer1=new DataOutputStream(socket1.getOutputStream());
                outputPlayer2=new DataOutputStream(socket2.getOutputStream());

                outputPlayer1.writeInt(PLAYER1);
                outputPlayer2.writeInt(PLAYER2);

                if((int)(Math.random()*2)>1){
                    outputPlayer1.writeInt(X);
                    outputPlayer2.writeInt(O);
                    turn=PLAYER2;
                }else {
                    outputPlayer1.writeInt(O);
                    outputPlayer2.writeInt(X);
                    turn=PLAYER1;
                }

                playChess();

                if(target==PLAYER1){
                    outputPlayer1.writeInt(-1);
                }else {
                    outputPlayer2.writeInt(-1);
                }

                outputPlayer1.writeInt(state);
                outputPlayer2.writeInt(state);
            }catch (IOException ex){
                ex.printStackTrace();
            }
        }

        //改变Ｘ或Ｏ
        public void changeTarget(){
            if(target==O){
                target=X;
            }else {
                target=O;
            }
        }

        //改变player
        public void changeTurn(){
            if(turn==PLAYER1){
                turn=PLAYER2;
            }else {
                turn=PLAYER1;
            }
        }

        //给客户端传递信息，下棋
        public void playChess(){
            try{
                while (state==CONTINUE){
                    if(turn==PLAYER1){
                        outputPlayer1.writeInt(CONTINUE);
                        int next=inputPlayer1.readInt();
                        map[next/3][next%3]=target;

                        outputPlayer2.writeInt(next);
                    }else {
                        outputPlayer2.writeInt(CONTINUE);
                        int next=inputPlayer2.readInt();
                        map[next/3][next%3]=target;

                        outputPlayer1.writeInt(next);
                    }

                    if (isFull()){
                        state=DRAW;
                    }else if (isWin(target)){
                        state=target;
                    }else {
                        changeTarget();
                        changeTurn();
                    }

                }
            }catch (IOException ex){
                ex.printStackTrace();
            }
        }

        //判断棋局是否已经满了
        private boolean isFull(){
            for(int i=0;i<map.length;i++){
                for(int j=0;j<map[i].length;j++){
                    if(map[i][j]!=0)
                        return false;
                }
            }

            return true;
        }

        //判断棋局是否有人赢了
        private boolean isWin(int target){
            //判断行列
            for(int i=0;i<map.length;i++){
                boolean isWin1=true;
                boolean isWin2=true;
                for(int j=0;j<map[i].length;j++){
                    isWin1=isWin1&&map[i][j]==target;
                    isWin2=isWin2&&map[j][i]==target;
                }
                if(isWin1||isWin2){
                    return true;
                }
            }

            //判断斜线
            if(map[0][0]==map[1][1]&&map[1][1]==map[2][2]
                    &&map[2][2]==target){
                return true;
            }

            if(map[2][0]==map[1][1]&&map[1][1]==map[0][2]
                    &&map[0][2]==target){
                return true;
            }

            return false;
        }
    }

    public static void main(String[] args) {
        new TicTacToeServer();
    }
}
